package com.buaa.xxx0322;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void startServer(View view) {

        new Thread() {
            @Override
            public void run() {
                try {
                    ServerSocket server = new ServerSocket(4440);
                    Socket sk = server.accept();
                    InputStream in = sk.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    Log.i("xxx", reader.readLine());
                    OutputStream out = sk.getOutputStream();
                    out.write("hello".getBytes("utf-8"));
                    out.close();
                    sk.close();
                }catch (Exception e) {

                }
            }
        }.start();

    }

    public void sendToServer(View view) {
        new Thread() {
            @Override
            public void run() {
                try {
                    Socket sk = new Socket("172.22.195.1",4440);
                    OutputStream out = sk.getOutputStream();
                    out.write("I am client\n".getBytes("utf-8"));

                    BufferedReader reader = new BufferedReader(new InputStreamReader(sk.getInputStream()));
                    Log.i("xxx", reader.readLine()+"..........");

                    reader.close();
                    out.close();
                    sk.close();
                }catch (Exception e) {

                }
            }
        }.start();
    }

    public void startUDP(View view) {
        new Thread() {
            @Override
            public void run() {
                try{
                    byte[] buf = new byte[1024];
                    DatagramSocket socket = new DatagramSocket(7777);
                    DatagramPacket p = new DatagramPacket(buf, 1024);
                    socket.receive(p);
                    byte[] data = p.getData();
                    String s = new String(data, 0, p.getLength());
                    Log.i("xxx", s);
                    InetAddress remoteAddr = p.getAddress();
                    int remotePort  = p.getPort();
                    DatagramPacket sendP = new DatagramPacket("okokok".getBytes(), 6,remoteAddr,remotePort);
                    socket.send(sendP);
                    socket.close();
                }catch (Exception e) {

                }
            }
        }.start();
    }

    public void sendToUDP(View view) {
        new Thread() {
            @Override
            public void run() {
                try {
                    DatagramSocket sk = new DatagramSocket();
                    String msg = "hello I am UDP";
                    InetAddress addr = InetAddress.getByName("127.0.0.1");
                    DatagramPacket p = new DatagramPacket(msg.getBytes(), msg.length(),
                            addr,7777);
                    sk.send(p);
                    byte[] buf = new byte[1204];
                    DatagramPacket recP = new DatagramPacket(buf, 1024);
                    sk.receive(recP);;
                    String ms = new String(recP.getData(), 0, recP.getLength());
                    Log.i("xxx", ms);
                    sk.close();
                }catch (Exception e) {

                }
            }
        }.start();
    }
}
